﻿<?php
error_reporting(0);
session_start();

include './classes/Auth.class.php';
include './includes/loadservers.php'; 
include './includes/parselang.php'; 

$pid = Battlelog\User::UserPID();
$dsnm = Battlelog\User::UserDSNM();
$clantag = Battlelog\User::UserCLNTG();
$ingame = Battlelog\User::UserINGAME();
$gravatar = Battlelog\User::UserGRAVATAR();

$cfg = parse_ini_file("./config.cfg");
$link = mysqli_connect($cfg['host'], $cfg['user'], $cfg['password'], $cfg['database']) or die("Error " . mysqli_error($link));

if($ingame)
{
	echo '<script type="text/javascript">gameManagerInGame();</script>';
}
else
{
	echo '<script type="text/javascript">gameManagerDisabled();</script>';
}

//Ingame
$queryInGame = 'SELECT pid FROM players WHERE `inGame` != 0 AND pid != '.$pid;
$resultInGame = $link->query($queryInGame);
$inGamePids = 0;

if (mysqli_num_rows($resultInGame)>0)
{
	$inGameCount = mysqli_num_rows($resultInGame);
}
else
{
	$inGameCount = 0;
}
//$inGameCount = $inGameCount + 1;	//+1 my player


//Get ServerName from GID
$queryServerNameFromGID = 'SELECT gid,gnam FROM gameservers';
$resultServerNameFromGID = $link->query($queryServerNameFromGID);

if (mysqli_num_rows($resultServerNameFromGID)>0) 
{ 
	while($row = mysqli_fetch_array($resultServerNameFromGID)) 
	{ 
		if(strlen($row['gnam']) > 25)
		{
			$ServerNameFromGID[$row['gid']] = substr($row['gnam'], 0, 20)."..";
		}
		else
		{
			$ServerNameFromGID[$row['gid']] = $row['gnam'];
		}
	}
}

 echo "<ul class='comcenter-list'>";
 //echo "<li class='comcenter-separator online showing-online '><span class='origin'></span></li>";
 if($ingame)
 { 
	echo "<li id='comcenter-2955060601802143325' class='comcenter-friend-item trigger-el comcenter-friend comcenter-friend-online comcenter-friend-playing'>";
	echo "<div class='comcenter-avatar'>";
    echo '<a class="avatar medium playing" href="javascript:void(0)" onclick="javascript:loadStat(\''.$pid.'\')" style="background-image: url(./img/avatars/'.$gravatar.');"></a>';
 }
 else
 {
	 echo "<li id='comcenter-2955060601802143325' class='comcenter-friend-item trigger-el comcenter-friend comcenter-friend-online'>";
	 echo "<div class='comcenter-avatar'>";
	 echo '<a class="avatar medium online" href="" style="background-image: url(./img/avatars/'.$gravatar.');"></a>';
 }
 echo "</div>";
 
 echo "<div class='comcenter-username '>";
 
 if ($clantag != '')
	echo "<a class='comcenter-username-link ignored-trigger-el' href=''>[".$clantag."]".$dsnm."</a>";
 else
	echo "<a class='comcenter-username-link ignored-trigger-el' href=''>".$dsnm."</a>";
 if($ingame)
 {
	echo "<div class='comcenter-username-serverinfo '>";
	echo "<span class='common-gameicon-hori bright common-game-2048-1 comcenter-game-icon'></span>";
	echo "<span class='common-playing-link'>";
	echo '<a title="'.$ServerNameFromGID[$ingame].'" class="common-playing-link base-no-ajax comcenter-playing-link" href="">'.$ServerNameFromGID[$ingame].'</a>';
	echo "</span>";
	echo "</div>";
 }
 echo "</div>";
  
 echo "<div class='comcenter-interact-container'>";
 if($ingame)
 {
	echo "<button class='join-friend-generic-button  btn btn-small join-friend-submit-link join-friend join-friendcomcenter-interact-playing  '>";
	echo "<i class='icon-join-friend'></i>";
	echo "</button>";
 }
  
 echo "<button class='comcenter-interact-chat'></button>
 </div>
 </li>";


if (mysqli_num_rows($resultInGame)>0)
{
	while($row = mysqli_fetch_array($resultInGame)) 
	{ 
		$inGamePids .= $row['pid'].",";
	}
	//Remove last ,
	$inGamePids = substr($inGamePids, 0, -1);

	$queryUsernames = 'SELECT clantag,dsnm,gravatar,pid,inGame FROM players WHERE pid IN ('.$inGamePids.') AND pid != '.$pid.' ';
	$resultUsernames = $link->query($queryUsernames);
	
	echo "<li id='comcenter-offline-separator' data-list-type='friends' class='comcenter-separator offline showing-offline clearfix'>".$lang['Ingame Players']." (".$inGameCount.")</li>";		
	while($usernames = mysqli_fetch_array($resultUsernames)) 
	{
		$params = '-webMode MP -Origin_NoAppFocus -onlineEnvironment prod -loginToken 0-0-0-0-0-1 -AuthToken 0-0-0-0-0-1 -requestState State_ClaimReservation -requestStateParams "<data putinsquad=\"true\" gameid=\"'.$usernames['inGame'].'\" personaref=\"'.$pid.'\" invitepersonaname=\"'.$usernames['dsnm'].'\" invitepersonaid=\"'.$usernames['pid'].'\" levelmode=\"mp\"></data>"|'.$hostIP.'|';
		$joinServerLink = "bf3Lan://".base64_encode ( $params );
		if ($usernames['gravatar'] == '')
			//$usernames['gravatar'] = "./img/default-avatar-36.png";
			$gravatar = "default-avatar-36.png";
		else
			$gravatar = $usernames['gravatar'];

		echo "<li id='comcenter-2955060601802143325' class='comcenter-friend-item trigger-el comcenter-friend comcenter-friend-online comcenter-friend-playing'>";
		echo "<div class='comcenter-avatar'>";
		echo '<a class="avatar medium playing" href="javascript:void(0)" onclick="javascript:loadStat(\''.$usernames['pid'].'\')" style="background-image: url(./img/avatars/'.$gravatar.');"></a>';
		//echo '<a class="avatar medium playing" href="javascript:void(0)" onclick="javascript:loadStat(\''.$usernames['pid'].'\')" style="background-image: url(./img/avatars/default-avatar-36.png);"></a>';
 		echo "</div>";
 
		echo "<div class='comcenter-username '>";
		
		if ($usernames['clantag']=='')
		{
			echo '<a class="comcenter-username-link ignored-trigger-el" href="javascript:void(0)" onclick="javascript:loadStat(\''.$usernames['pid'].'\')">'.$usernames['dsnm'].'</a>';
		}
		else
		{
			echo '<a class="comcenter-username-link ignored-trigger-el" href="javascript:void(0)" onclick="javascript:loadStat(\''.$usernames['pid'].'\')">['.$usernames['clantag'].']'.$usernames['dsnm'].'</a>';
		}
		
		
		echo "<div class='comcenter-username-serverinfo '>";
		echo "<span class='common-gameicon-hori bright common-game-2048-1 comcenter-game-icon'></span>";
		//echo "<span class='common-playing-link' style='margin-left:-36px;'>";
		echo "<span class='common-playing-link'>";
		echo '<a title="'.$ServerNameFromGID[$usernames['inGame']].'" class="common-playing-link base-no-ajax comcenter-playing-link" href="">'.$ServerNameFromGID[$usernames['inGame']].'</a>';
		echo "</span>";
		echo "</div>";
		echo "</div>";
  
		echo "<div class='comcenter-interact-container'>";
 		echo '<button class="join-friend-generic-button  btn btn-small join-friend-submit-link join-friend join-friendcomcenter-interact-playing  " href="javascript:void(0)" onclick="javascript:startBF3(\''.$joinServerLink.'\')">';
		echo "<i class='icon-join-friend'></i>";
		echo "</button>";
 		echo "<button class='comcenter-interact-chat'></button>
		</div>
		</li>";
	}
	
}

//Online
$queryUsernames = 'SELECT clantag,dsnm,gravatar,pid FROM players WHERE (now() - time) < 90 AND pid != '.$pid.' AND pid NOT IN ('.$inGamePids.')';
$resultUsernames = $link->query($queryUsernames);
	
if (mysqli_num_rows($resultUsernames) > 0)
{

	//echo "<li class='comcenter-separator online showing-online '><span class='origin'>Online Players (".mysqli_num_rows($resultUsernames).")</span></li>";
	
	echo "<li id='comcenter-offline-separator' data-list-type='friends' class='comcenter-separator offline showing-offline clearfix'>".$lang['online players']. "(".mysqli_num_rows($resultUsernames).")</li>";
	
	while($usernames = mysqli_fetch_array($resultUsernames)) 
	{ 
		$gravatar = $usernames['gravatar'];
		
		/*if ($gravatar == '')
		{
			$gravatar = "img/default-avatar-36.png";
		}
		*/
		
 		echo "<li id='comcenter-2955060601802143325' class='comcenter-friend-item trigger-el comcenter-friend comcenter-friend-online'>";
		echo "<div class='comcenter-avatar'>";
		//echo "<a class='avatar medium online' href='' style='background-image: url(./img/default-avatar-36.png);'></a>";
		
		
		echo '<a class="avatar medium online" href="javascript:void(0)" onclick="javascript:loadStat(\''.$usernames['pid'].'\')" style="background-image: url(img/avatars/default-avatar-36.png);"></a>';
		
 		echo "</div>";
 		echo "<div class='comcenter-username '>";
		//echo "<a class='comcenter-username-link ignored-trigger-el' href=''>".$usernames['dsnm']."</a>";
		
		
		if ($usernames['clantag']=='')
		{
			echo '<a class="comcenter-username-link ignored-trigger-el" href="javascript:void(0)" onclick="javascript:loadStat(\''.$usernames['pid'].'\')">'.$usernames['dsnm'].'</a>';
		}
		else
		{
			echo '<a class="comcenter-username-link ignored-trigger-el" href="javascript:void(0)" onclick="javascript:loadStat(\''.$usernames['pid'].'\')">['.$usernames['clantag'].']'.$usernames['dsnm'].'</a>';
		}
		
		
		echo "</div>";
		echo "<div class='comcenter-interact-container'>";
		echo "<button class='comcenter-interact-chat'></button>
		</div>
		</li>";
	
	}
}

//Offline


$queryUsernamesOff = 'SELECT clantag,dsnm,gravatar,pid FROM players WHERE (now() - time) >= 90 AND pid != '.$pid.' AND pid NOT IN ('.$inGamePids.') ORDER BY dsnm';
$resultUsernamesOff = $link->query($queryUsernamesOff);

if (mysqli_num_rows($resultUsernamesOff) > 0)
{

//echo "<li class='comcenter-separator online showing-online '><span class='origin'>Online Players (".mysqli_num_rows($resultUsernames).")</span></li>";

echo '<li id="comcenter-offline-separator" data-list-type="friends" class="comcenter-separator offline showing-offline clearfix"><span onclick="javascript:showHide(\'offline\')">' .$lang['offlineplayers'].' ('.mysqli_num_rows($resultUsernamesOff).')</span></li>';

echo '<div id="offline" style="display:none;r">'; 
while($usernames = mysqli_fetch_array($resultUsernamesOff))
{
$gravatar = $usernames['gravatar'];

/*if ($gravatar == '')
{
$gravatar = "img/default-avatar-36.png";
}
*/

echo "<li id='comcenter-2955060601802143325' class='comcenter-friend-item trigger-el comcenter-friend comcenter-friend-offline'>";
echo "<div class='comcenter-avatar'>";
//echo "<a class='avatar medium online' href='' style='background-image: url(./img/default-avatar-36.png);'></a>";

echo '<a class="avatar medium offline" href="javascript:void(0)" onclick="javascript:loadStat(\''.$usernames['pid'].'\')" style="background-image: url(./img/avatars/default-avatar-36.png);"></a>';

echo "</div>";
echo "<div class='comcenter-username '>";
//echo "<a class='comcenter-username-link ignored-trigger-el' href=''>".$usernames['dsnm']."</a>";


if ($usernames['clantag']=='')
{
echo '<a class="comcenter-username-link ignored-trigger-el" href="javascript:void(0)" onclick="javascript:loadStat(\''.$usernames['pid'].'\')">'.$usernames['dsnm'].'</a>';
}
else
{
echo '<a class="comcenter-username-link ignored-trigger-el" href="javascript:void(0)" onclick="javascript:loadStat(\''.$usernames['pid'].'\')">['.$usernames['clantag'].']'.$usernames['dsnm'].'</a>';
}


echo "</div>";
echo "<div class='comcenter-interact-container'>";
echo "
</div>
</li>";

}
echo '</div>';
}
	
echo "</ul>";

mysqli_close($link);
?>	
